﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Heroius.Extension;

namespace Heroius.XAMLHelper
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            context = new Context();
        }

        Context context;

        private void BtnRenderClick(object sender, RoutedEventArgs e)
        {
            try
            {
                StringBuilder xaml = new StringBuilder();
                xaml.AppendFormat("<Grid {0}>", context.NameSpaces.Select(ns=>{
                    if(string.IsNullOrWhiteSpace( ns.Key))
                        return string.Format("xmlns=\"{0}\"", ns.Value);
                    else
                        return string.Format("xmlns:{0}=\"{1}\"", ns.Key, ns.Value);
                }).Merge(" "));
                xaml.Append(TbRender.Text);
                xaml.Append("</Grid>");
                UIElement obj = (UIElement)XamlReader.Parse(xaml.ToString());
                GrdRender.Children.Clear();
                GrdRender.Children.Add(obj);
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("转换错误: {0}", ex.Message));
            }
        }

        private void BtnSetClick(object sender, RoutedEventArgs e)
        {
            new WindowSet(context).ShowDialog();
        }
    }
}
